package com.ruoyi.controller;


import com.ruoyi.domain.DocumentCategory;
import com.ruoyi.dto.CategoryTree;
import com.ruoyi.platform.annotation.Log;
import com.ruoyi.platform.constant.UserConstants;
import com.ruoyi.platform.core.controller.BaseController;
import com.ruoyi.platform.core.domain.AjaxResult;
import com.ruoyi.platform.core.domain.R;
import com.ruoyi.platform.enums.BusinessType;
import com.ruoyi.platform.utils.StringUtils;
import com.ruoyi.platform.utils.poi.ExcelUtil;
import com.ruoyi.service.IDocumentCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文档目录Controller
 *
 * @author tp
 * @date 2022-11-02
 */
@RestController
@RequestMapping("/admin/documentCategory")
@Api(tags = {"文档目录服务"})
public class DocumentCategoryController extends BaseController {

    @Autowired
    private IDocumentCategoryService documentCategoryService;

    /**
     * 查询文档目录列表
     */
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询文档目录列表", httpMethod = "GET", notes = "查询文档目录列表")
    public AjaxResult list(DocumentCategory documentCategory) {
        List<DocumentCategory> list = documentCategoryService.selectDocumentCategoryList(documentCategory);
        return AjaxResult.success(list);
    }

    /**
     * 查询文档列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:list')")
    @GetMapping("/exclude")
    public AjaxResult excludeChild(@RequestParam(value = "id", required = false) Long id) {
        List<DocumentCategory> list = documentCategoryService.selectDocumentCategoryList(new DocumentCategory());
        list.removeIf(d -> d.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""));
        return AjaxResult.success(list);
    }

    /**
     * 导出文档目录列表
     */
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:export')")
    @Log(title = "文档目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出文档目录列表", httpMethod = "POST", notes = "导出文档目录列表")
    public void export(HttpServletResponse response, DocumentCategory documentCategory) {
        List<DocumentCategory> list = documentCategoryService.selectDocumentCategoryList(documentCategory);
        ExcelUtil<DocumentCategory> util = new ExcelUtil<>(DocumentCategory.class);
        util.exportExcel(response, list, "文档目录数据");
    }

    /**
     * 获取文档目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取文档目录详细信息", httpMethod = "GET", notes = "获取文档目录详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(documentCategoryService.selectDocumentCategoryById(id));
    }

    /**
     * 新增文档目录
     */
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:add')")
    @Log(title = "文档目录", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增文档目录", httpMethod = "POST", notes = "新增文档目录")
    public AjaxResult add(@Validated @RequestBody DocumentCategory documentCategory) {
        if (UserConstants.NOT_UNIQUE.equals(documentCategoryService.checkCategoryNameUnique(documentCategory))) {
            return AjaxResult.error("新增文档目录'" + documentCategory.getCategoryName() + "'失败，文档目录已存在");
        }
        return toAjax(documentCategoryService.insertDocumentCategory(documentCategory));
    }

    /**
     * 修改文档目录
     */
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:edit')")
    @Log(title = "文档目录", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改文档目录", httpMethod = "PUT", notes = "修改文档目录")
    public AjaxResult edit(@Validated @RequestBody DocumentCategory documentCategory) {
        return toAjax(documentCategoryService.updateDocumentCategory(documentCategory));
    }

    /**
     * 删除文档目录
     */
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:remove')")
    @Log(title = "文档目录", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(documentCategoryService.deleteById(id));
    }

    /**
     * 批量删除文档目录
     */
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:remove')")
    @Log(title = "文档目录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除文档目录", httpMethod = "DELETE", notes = "批量删除文档目录")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(documentCategoryService.deleteDocumentCategoryByIds(ids));
    }

    /**
     * 加载文档目录树
     */
    @GetMapping("/treeData")
    @PreAuthorize("@ss.hasPermi('admin:documentCategory:query')")
    @ApiOperation(value = "加载文档目录树", httpMethod = "GET", notes = "加载文档目录树")
    public R treeData() {
        List<CategoryTree> treeList = documentCategoryService.getTreeList(new DocumentCategory());
        return R.ok(treeList);
    }

}
